<?php
App::uses('AppModel', 'Model');

class Qrcodeing extends AppModel {
	
	public $useTable = false;

	function getET($order_list_id){
		$hex = md5($order_list_id);
		$floadF = substr($hex, 0, 3);
		
		$filePath = 'img/static';
		$filePath .= '/'.$floadF;
		$filePath .= '/'.$hex.'_e_ticket.png';
		return subStr($filePath,4,strlen($filePath)-4);
	}
	/**
	 * 生成qrcode
	 * @author zmx@gootop.net
	 * @Date 2014-3-22
	 * @return
	 */
	function createQRCode($order_list_id,$URL = false){
		
			$this->autoRender = false;
			App::import('Vendor', 'QRLIB', array('file' => 'Qrcode/qrlib.php'));
			//创建存储目录
			$hex = md5($order_list_id);
			$floadF = substr($hex, 0, 3);
		
			$filePath = 'img/static';
			$filePath .= '/'.$floadF;
			if (!is_dir(WWW_ROOT.$filePath)){
				mkdir ($filePath);
			}
			$filePath .= '/'.$hex.'_e_ticket.png';
			$filename = $filePath;
		
			//设置容错等级
			//array('L','M','Q','H')
			$errorCorrectionLevel = 'M';
		
			//martix点值
			$matrixPointSize = 6;
		
			$qrdata = $URL;
			QRcode::png($qrdata, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
			$this->mark(WWW_ROOT.$filename);
			return subStr($filename,4,strlen($filename)-4);
	}
	
	
	/**
	 * 水印
	 * @author mingxing.zhao@qq.com
	 * @date 2014-3-22 下午3:23:04
	 * @param variable
	 * @return 
	 */
	public function mark($filename){
		$base_path = WWW_ROOT.'img'.DS.'qrmark'.DS;
		$logo = $base_path.'logo.png';
		if(is_file($logo))
		{
			$QR = imagecreatefromstring(file_get_contents($filename));
			$logo = imagecreatefromstring(file_get_contents($logo));
			$QR_width = imagesx($QR);
			$QR_height = imagesy($QR);
			$logo_width = imagesx($logo);
			$logo_height = imagesy($logo);
			$logo_qr_width = $QR_width / 5;
			$scale = $logo_width / $logo_qr_width;
			$logo_qr_height = $logo_height / $scale;
			$from_width = ($QR_width - $logo_qr_width) / 2;
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
			imagepng($QR,$filename);
		}
	}
}
?>
